package com.bop.dehong.util;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Set;
import java.util.TreeMap;

import android.app.Activity;
import android.text.TextUtils;

import com.bop.dehong.config.Config;
import com.base.dehong.util.LogUtils;
import com.base.dehong.util.MD5Util;
import com.base.dehong.util.PreferencesUtils;

/**
* @author TangWenhan/YeFengyun
*2015年11月24日下午4:48:19
*/
public class WebUrlUtil {
	
	public static String getWebUrl(Activity mActivity, String url, TreeMap<String, String> postValues ){
		Long current = System.currentTimeMillis() / 1000;
		Long modify = PreferencesUtils.getLong(mActivity, "tag");
		Long mTimestamp = current + modify;
		int index = (int) (mTimestamp / 10 % 10);
		postValues.put("stamp" , mTimestamp + "");
		Set<Entry<String, String>> entrySet = postValues.entrySet();
		List<Map.Entry<String, String>> paramList =
			    new ArrayList<Map.Entry<String, String>>(entrySet);
		StringBuilder macStr = new StringBuilder();
		StringBuilder urlSb = new StringBuilder();
		urlSb.append(url).append("?");
		for (int i = 0; i < paramList.size(); i++) {
			String key = paramList.get(i).getKey();
			String value = paramList.get(i).getValue();
			if (TextUtils.isEmpty(value)) {
				value = "";
			}
			if ((!key.equals("a")) && (!key.equals("m"))) {
				urlSb.append(key);
				urlSb.append("=");
				urlSb.append(value);
				urlSb.append("&");
			}
			macStr.append(key);
			macStr.append("=");
			macStr.append(value);
			macStr.append("&");
		}
		macStr.deleteCharAt(macStr.length() - 1);
		macStr.append(Config.token_key[index]);
//		macStr.append(Config.token_key[0]);
		String md5Str = MD5Util.getMD5String(macStr.toString());
		urlSb.append("bd_sig=").append(md5Str);
		String url2 = urlSb.toString();
		LogUtils.e("url:" + url2);
		return url2;
	}
}

